import { Effect, Reducer } from 'umi';
import { accountLogin } from '@/services/login';

export interface LoginModelState {
  staus?: string;
  token?: string;
  userinfo?: {};
}

export interface LoginModelType {
  namespace: string;
  state: LoginModelState;
  effects: {
    login: Effect;
    logout: Effect;
  };
  reducers: {
    changeLoginStatus: Reducer<LoginModelState>;
  };
}

const LoginModel: LoginModelType = {
  namespace: 'login',
  state: {
    userinfo: {},
  },
  effects: {
    *login({ payload }, { call, put }) {
      const response = yield call(accountLogin, payload);
      yield put({
        type: 'changeLoginStatus',
        payload: response,
      });
    },
    *logout({ payload }) {},
  },
  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        status: payload.status,
        token: payload.token,
      };
    },
  },
};

export default LoginModel;
